package test;

import com.alibaba.fastjson.JSON;
import entity.Foods;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;

/***************************
 FileAction:

 CreateTime:2022-05-20 14:08
 Author:Aaking-aline
 ****************************/
@WebServlet("/GetServlet")
public class GetServlet extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		URL url = new URL("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1");

		InputStream is = url.openStream();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader bufR = new BufferedReader(isr);

		String allJson = "";
		String tmpJson = "";
		while((tmpJson = bufR.readLine()) != null){
			allJson = allJson + tmpJson;
		}

		bufR.close();
		isr.close();
		is.close();

		System.out.println(allJson);

		FileWriter fw = new FileWriter(new File("D:/J-regional-temp/zy4/json.txt"));
		fw.write(allJson);
		fw.flush();
		fw.close();

		System.out.println("--------------------------------------------------");

		Foods foods = JSON.parseObject(allJson, Foods.class);

		System.out.println(foods);


		request.setAttribute("json",foods);
		request.getRequestDispatcher("Result.jsp").forward(request,response);



	}
}
